package com.aia.prm.web.ria.app.ui.plan;

import java.util.List;

import org.apache.log4j.Logger;

import com.aia.prm.BeanFactory;
import com.aia.prm.domain.VOPlan;
import com.aia.prm.persistence.dao.VOPlanDAO;
import com.aia.prm.web.ria.app.PrmApplication;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.VerticalSplitPanel;

@SuppressWarnings("serial")
public class PlanView extends VerticalLayout implements Property.ValueChangeListener {
	private static final Logger logger = Logger.getLogger(PlanView.class);
	private PrmApplication app = null;
	VerticalSplitPanel vert = null;
	HorizontalSplitPanel horiz = null;
	VerticalLayout rightLayout = null;
	PlanList PlanList = null;
	Form form = null;
	PlanContainer PlanContainer = null;
	PlanForm PlanForm = null;
	private VOPlanDAO planDAO = null;
	List<VOPlan> list = null;

	VerticalLayout topLayout = null;
	Button newButton = null;

	public PlanView(PrmApplication aapp) {
		app = aapp;
		setPlanDAO((VOPlanDAO) BeanFactory.get().getBean("vOPlanDAO"));
		buildMainLayout();
		load();
	}

	void load() {
		list = getPlanDAO().selectByExample(null);
		if (PlanContainer == null) {
			try {
				PlanContainer = new PlanContainer();
			} catch (Throwable e) {
				logger.error(e);
				throw new RuntimeException(e);
			}
		}

		PlanContainer.removeAllItems();
		PlanContainer.addAll(list);

		PlanList.setContainerDataSource(PlanContainer);

		PlanList.setVisibleColumns(PlanContainer.NATURAL_COL_ORDER);
		PlanList.setColumnHeaders(PlanContainer.COL_HEADERS_SHOW);

		PlanForm.setItemDataSource(null);
	}

	private void buildMainLayout() {
		setSizeFull();
		vert = new VerticalSplitPanel();
		vert.setSizeFull();
		vert.setSplitPosition(60, Sizeable.UNITS_PIXELS);
		addComponent(vert);

		// add a label to the upper area
		topLayout = new VerticalLayout();
		topLayout.setSpacing(true);
		newButton = new Button("添加记录", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				PlanForm.addObject();
			}
		});

		newButton.setIcon(new ThemeResource("icons/add.png"));
		HorizontalLayout topLayoutSub = new HorizontalLayout();

		topLayoutSub.setSizeUndefined();
		topLayoutSub.setMargin(true);
		topLayoutSub.setSpacing(true);

		topLayoutSub.addComponent(newButton);
		topLayout.addComponent(topLayoutSub);
		vert.setFirstComponent(topLayout);

		horiz = new HorizontalSplitPanel();
		horiz.setSplitPosition(66); // percent
		vert.setSecondComponent(horiz);

		// left component:
		PlanList = new PlanList(app, this);
		horiz.setFirstComponent(PlanList);

		rightLayout = new VerticalLayout();
		rightLayout.setSizeUndefined();
		rightLayout.setWidth("100%");
		rightLayout.setMargin(true);
		PlanForm = new PlanForm(this);

		rightLayout.addComponent(PlanForm);
		// right component:
		horiz.setSecondComponent(rightLayout);

	}

	public void valueChange(ValueChangeEvent event) {
		Item item = PlanList.getItem(PlanList.getValue());
		PlanForm.setItemDataSource(item);
	}

	public VOPlanDAO getPlanDAO() {
		return planDAO;
	}

	public void setPlanDAO(VOPlanDAO planDAO) {
		this.planDAO = planDAO;
	}

}
